package com.starbaba.activity.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.activity.model.LoanHandPhoneConfigModel;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;

@SuppressWarnings({"deprecation", "rawtypes"})
@Component
public class LoanHandPhoneConfigService {

	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	public void showPage(Map<String, Object> paramsMap) throws Exception{
		
		List<LoanHandPhoneConfigModel> activityList = dao
				.list("select a.activity_id, b.name activity_name from loan_handphone_config a, loan_product_info b where a.activity_id = b.id group by a.activity_id ",
						LoanHandPhoneConfigModel.class);
		paramsMap.put("activityList", activityList);
	}
	
	public void showEditPage(Map<String, Object> paramsMap) throws Exception{
		
		List<LoanHandPhoneConfigModel> activityList = dao
				.list("select id activity_id, name activity_name from loan_product_info where status = 1",
						LoanHandPhoneConfigModel.class);
		paramsMap.put("activityList", activityList);
		String id = (String) paramsMap.get("id");
		LoanHandPhoneConfigModel mode = dao.get("select * from loan_handphone_config where id = ? ", LoanHandPhoneConfigModel.class, id);
		if (mode == null) {
			paramsMap.put("model", new LoanHandPhoneConfigModel());
		} else {
			paramsMap.put("model", mode);
		}
		
		
	}
	
	public List<LoanHandPhoneConfigModel> getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String activity = (String) paramsMap.get("activity");
		String status = (String) paramsMap.get("status");
		String remark = (String) paramsMap.get("remark");
		String startAmount = (String) paramsMap.get("start_amount");
		String endAmount = (String) paramsMap.get("end_amount");
		String activity_type = (String) paramsMap.get("activity_type");
		
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startAmount)) {
			whereParams.append("and a.amount >= " + startAmount + " ");
		}
		if (StringUtils.isNotEmpty(endAmount)) {
			whereParams.append("and a.amount <= " + endAmount + " ");
		}
		if (StringUtils.isNotEmpty(activity)) {
			whereParams.append("and a.activity_id = '" + activity + "' ");
		}
		if (StringUtils.isNotEmpty(status)) {
			whereParams.append("and a.status = '" + status + "' ");
		}
		if (StringUtils.isNotEmpty(remark)) {
			whereParams.append("and a.remark like '%" + remark + "%' ");
		}
		if (StringUtils.isNotEmpty(activity_type)) {
			whereParams.append("and a.activity_type = " + activity_type + " ");
		}
		
		StringBuilder listSql = new StringBuilder("select a.*, b.name activity_name from loan_handphone_config a, loan_product_info b where a.activity_id = b.id ");
		listSql.append(whereParams).append("order by utime desc ");
		String sqlCount = "select count(1) from (" + listSql.toString() + ") z ";
		if (start != null && length != null) {
			listSql.append("limit " + start + ", " + length);
		}
		int count = dao.getInt(sqlCount);
		List<LoanHandPhoneConfigModel> list = dao.list(listSql.toString(), LoanHandPhoneConfigModel.class);
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		return list;
	}
	
	public String editInfo(Map<String, Object> paramsMap) throws Exception {

		String message = "";
		String id = (String) paramsMap.get("id");
		String amount = (String) paramsMap.get("amount");
		String activity = (String) paramsMap.get("activity");
		String activity_type = (String) paramsMap.get("activity_type");
		String remark = (String) paramsMap.get("remark");
		String off = (String) paramsMap.get("off");
		String tag_type = (String) paramsMap.get("tag_type");

		if (StringUtils.isEmpty(id)) {
			if (!getHandPhoneActivity(activity, activity_type)) {
				// 新增
				LoanHandPhoneConfigModel model = new LoanHandPhoneConfigModel(Integer.valueOf(amount), 
						Long.valueOf(activity), Integer.valueOf(activity_type), 1, remark,tag_type);
				dao.add(model);
				message = "新增成功";
			} else {
				message = "活动已存在";
			}
		} else if (StringUtils.isNotEmpty(off)) {
			// 上下线
			LoanHandPhoneConfigModel model = dao.get("select * from loan_handphone_config "
					+ " where id = ? ", LoanHandPhoneConfigModel.class, id);
			if (model.getStatus() == 1) {
				model.setStatus(0);
				dao.update(model, "id");
				message = "下线成功";
			} else if (model.getStatus() == 0) {
				model.setStatus(1);
				dao.update(model, "id");
				message = "上线成功";
			}
		} else {
			// 更新
			LoanHandPhoneConfigModel model = new LoanHandPhoneConfigModel();
			if (StringUtils.isNotEmpty(amount)) {
				model.setAmount(Integer.valueOf(amount));
			}
			if (StringUtils.isNotEmpty(remark)) {
				model.setRemark(remark);
			}
			if (StringUtils.isNotEmpty(tag_type)) {
				model.setTag_type(tag_type);
			}
			model.setId(Long.valueOf(id));
			dao.update(model, "id");
			message = "更新成功";
		}
		return message;
	}

	/**
	 * 查看活动是否存在
	 * @param activity
	 * @param activity_type
	 * @return
	 * @throws AppException 
	 */
	public boolean getHandPhoneActivity(String activity, String activity_type) throws AppException {

		LoanHandPhoneConfigModel model = new LoanHandPhoneConfigModel();
		model.setActivity_id(Long.parseLong(activity));
		model.setActivity_type(Integer.parseInt(activity_type));
		return dao.get(model, new String[] { "activity_id", "activity_type"}) != null;
	}
}
